﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;
using System.Text.RegularExpressions;
using QLTT.Properties;

namespace QLTT.ViewModels
{
    class RoomViewModel : WorkspaceViewModel, System.ComponentModel.IDataErrorInfo
    {
        private Room _room;
        private icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _editCommand;
        private DelegateCommand _cancelCommand;
                
        private bool _isSelected;
        public RoomViewModel(icanDataContext dataContext)
        {
            _dataContext = dataContext;
            _room = new Room();
            base.DisplayName = Properties.Resources.Base_DisplayName_Room_New;
        }
        public RoomViewModel(icanDataContext dataContext,Room room)
        {
            _room = room;
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_Room_Edit;
        }
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save);
                }
                return _saveCommand;
            }
        }
        public ICommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new DelegateCommand(Edit);
                }
                return _editCommand;
            }
        }
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }

        private void Save()
        {
            if (_dataContext == null || _room == null)
                return;
            string[] properties = { "Name", "Capacity" };
            if (!ValidateData(properties))
                return;
            try
            {
                bool isInsert = _room.ID == 0;
                if (isInsert)
                {
                    _dataContext.Rooms.InsertOnSubmit(_room);
                }
                _dataContext.SubmitChanges();
                if (isInsert)
                    Mediator.NotifyColleagues<Room>(Messages.ListRoomUpdated, _room);
                Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
            
            
        }
        private void Edit()
        {
            if (_dataContext == null || _room == null)
                return;
            
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, this);
        }
        private void Cancel()
        {
            if(_room.ID > 0)
                _dataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _room);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        public void Delete()
        {
            if (_dataContext == null || _room == null)
                return;
            try
            {
                _dataContext.Rooms.DeleteOnSubmit(_room);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
        public bool CanDelete
        {
            get { return _room.Classes.Count == 0; }
        }

        public long ID
        {
            get { return _room.ID; }
        }

        public string Name
        {
            get { return _room.Name; }
            set
            {
                if (_room.Name == value) return;
                _room.Name = value;
                base.OnPropertyChanged("Name");
            }
        }
        public Nullable<short> Capacity
        {
            get { return _room.Capacity; }
            set
            {
                if (_room.Capacity ==value) return;
                _room.Capacity = value;
                base.OnPropertyChanged("Capacity");
            }
        }
        public List<Class> Classes
        {
            get { return _room.Classes.ToList(); }

        }
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                base.OnPropertyChanged("IsSelected");
            }
        }
        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string property]
        {
            get
            {
                string msg = null;
                switch (property)
                {
                    case "Name":
                        if (string.IsNullOrEmpty(this.Name))
                            msg = Resources.ValidationRoomName_Error;
                        break;
                    case "Capacity":
                            if(this.Capacity == null || this.Capacity < 1 || this.Capacity>100)
                            msg = Resources.ValidationRoomCapacity_Error;
                        break;
                    default:
                        throw new ArgumentException(
                            Resources.ValidationDefaultException_Error + property);
                }
                return msg;
            }
        }

        #endregion // IDataErrorInfo Members

    }
}

